package com.me.db.domain;

import java.util.ArrayList;
import java.util.List;

public class PageModel<T> {

	private int pageNum = 1; // 当前页数据数
	private int pageIndex = 1; // 当前页码
	private int totalPages = 0; // 总页数
	private int totalRows = 0; // 总数据数
	private boolean hasNextPage = false; // 是否有下一页
	private boolean hasPreviousPage = false; // 是否有前一页
	private List<T> list;

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public PageModel(List<T> list, int pageSize, int pageIndex) {
		// TODO Auto-generated constructor stub
		if (list != null && list.size() > 0) {
			if (pageSize > 0) {
				this.pageNum = pageSize;
				this.totalPages = (int) Math.ceil((double) list.size() / pageSize);
			}
			this.totalRows = list.size();
			if (pageIndex > 0 && pageIndex <= totalPages) {
				this.pageIndex = pageIndex;
			} else {
				this.pageIndex = pageIndex;
				this.hasNextPage = false;
				this.hasPreviousPage = false;
				this.pageNum = 0;
				this.list = new ArrayList<>();
				return;
			}

			this.totalRows = list.size();
			if (pageIndex == 1) {
				this.hasPreviousPage = false;
			} else {
				this.hasPreviousPage = true;
			}

			if (pageIndex == totalPages) {
				this.hasNextPage = false;
			} else {
				this.hasNextPage = true;
			}

			int start = pageSize * (pageIndex - 1);
			int end = pageSize * pageIndex;
			List<T> lists = null;
			if (pageIndex != totalPages) {
				lists = list.subList(start, end);
			} else {
				lists = list.subList(start, list.size());
			}

			this.list = lists;
		}
	}

	public PageModel() {
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public List<T> getList() {

		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

}
